
***********************
********* TABLE 1 *****
***********************


clear 

use "$P_Data_Processed/monthly_station_data_e5_merged.dta"

cd "$P_Tables"
quietly ivreghdfe mean_wh_margin postal_n_stations n_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019, absorb(year_month StID) cluster(Post) 

sutex2 mean_price mean_wh_margin l_pop pop_density med_age l_gdp employed_share mean_temp sd_temp mean_precip sd_precip if e(sample), varlab saving(Table_1.tex) replace nocheck 
	
quietly ivreghdfe mean_price n_act_stations n_mkt_compet_adopt  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat =  at_least_10mbps tot_n_meas_wifi avg_down_speed_wifi avg_up_speed_wifi ) if year<2019, absorb(year_month StID) cluster(mktid) 
	
sutex2 at_least_10mbps if e(sample), varlab saving(Table_1.tex) append 

cd "$P_Main"

***********************
********* TABLE 2 *****
***********************


clear 

use "$P_Data_Processed/monthly_station_data_e5_merged.dta"


quietly reghdfe mean_price treat n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip   if year<2019 , absorb(year_month StID) cluster(mktid) 

preserve 
	cd "$P_Tables"
	keep if e(sample)==1

	sort StID year_month
	by StID: keep if _n==1
	keep StID mktid 
	merge m:1 StID using "$P_Data/GS/gas_station.dta"
	keep if _merge==3
	drop _merge

	* n stations per brand 
	sort Brand StID
	by Brand: gener n_brand_stations = _N
	by Brand: gener use_brand = 1 if _n==1
	gener one_station_brand = (n_brand_stations==1)
	tab one_station_brand if use_brand==1 
	sum n_brand_stations if use_brand==1 & n_brand_stations<=100 & one_station_brand==0

	label variable n_brand_stations "Stations per Brand"
	sutex2 n_brand_stations if use_brand==1 & one_station_brand==0, varlab min percentiles(25 75) digits(0) saving(Table_2.tex) replace 

	* n stations per market 
	sort mktid StID
	by mktid: gener n_mkt_stations = _N
	by mktid: gener use_mkt = 1 if _n==1
	sum n_mkt_stations if use_mkt==1  
	label variable n_mkt_stations "Stations per Market"
	sutex2 n_mkt_stations if use_mkt==1, varlab min percentiles(25 75) digits(0) saving(Table_2.tex) append 

	* n stations per ZIP code 
	sort Post StID
	by Post: gener n_zip_stations = _N
	by Post: gener use_zip = 1 if _n==1
	sum n_zip_stations if use_zip==1
	label variable n_zip_stations "Stations per 5 Digit ZIP Code"
	sutex2 n_zip_stations if use_zip==1, varlab min percentiles(25 75) digits(0) saving(Table_2.tex) append

	merge 1:1 StID using "$P_Data_Processed/nearest_neighbor.dta"
	keep if _merge==3
	drop _merge
	label variable Dist_km "Distance to nearest station (KM)" 
	sutex2 Dist_km , varlab min percentiles(25 75) digits(2) saving(Table_2.tex) append


restore 

cd "$P_Main"


******************  
*** TABLE 3 *** 
****************** 
** station level balancing 

* in 2016, are stations that eventually adopted similar to stations that did not eventually adopt? 
reg treat_group pop_density l_pop med_age employed_share postal_n_stations l_gdp i.year_month if treat==0 & year==2016, vce(cluster mktid)

outreg2 using "$P_Tables/Table_3", replace tex(frag) keep(pop_density l_pop med_age employed_share postal_n_stations l_gdp)  dec(5) label
 


******************  
*** TABLE 4 *** 
****************** 

label variable treat "Adopter $\times$ post-Adoption"

reghdfe mean_wh_margin treat n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip   if year<2019 , absorb(year_month StID) cluster(mktid) 
	sum mean_wh_margin if treat_group==0 
	scalar outcome_var_mean = r(mean) 
	outreg2 using "$P_Tables/Table_4", replace tex(frag) dec(3) label  keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)
	
reghdfe mean_price treat n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip   if year<2019 , absorb(year_month StID) cluster(mktid) 
	sum mean_price if  treat_group==0  
	scalar outcome_var_mean = r(mean) 
	outreg2 using "$P_Tables/Table_4", append tex(frag) dec(3) label   keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

	

label variable treat_m6month1 "Adopter $\times$ 1-6 Months pre-Adoption"
label variable treat_m6month2 "Adopter $\times$ 7-12 Months pre-Adoption"
label variable treat_m6month3 "Adopter $\times$ 13+ Months pre-Adoption"
	
reghdfe mean_wh_margin treat treat_m* postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019 & age>12, absorb(year_month StID) cluster(mktid)
	sum mean_wh_margin if treat_group==0  
	scalar outcome_var_mean = r(mean) 
	outreg2 using "$P_Tables/Table_4", append tex(frag) dec(3) label  keep(treat treat_m6month1 treat_m6month2 treat_m6month3)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

reghdfe mean_price treat treat_m* postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019  & age>12, absorb(year_month StID) cluster(mktid)
	sum mean_price if treat_group==0  
	scalar outcome_var_mean = r(mean) 
	outreg2 using "$P_Tables/Table_4", append tex(frag) dec(3) label  keep(treat treat_m6month1 treat_m6month2 treat_m6month3)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

******************  
*** TABLE 5 *** 
****************** 


* 1st stage
label variable share_others_treated "Share Brand Adopters"
	
reghdfe treat share_others_treated postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019, absorb(year_month StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_5", replace tex(frag) dec(3) label  keep(share_others_treated)  

* 2sls 
ivreghdfe mean_wh_margin postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = share_others_treated ) if year<2019, absorb(year_month StID) cluster(mktid)
	sum mean_wh_margin if treat_group==0  
	scalar outcome_var_mean = r(mean) 
outreg2 using "$P_Tables/Table_5", append tex(frag) dec(3) label  keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

ivreghdfe mean_price postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = share_others_treated ) if year<2019, absorb(year_month StID) cluster(mktid)
	sum mean_price if treat_group==0  
	scalar outcome_var_mean = r(mean) 
outreg2 using "$P_Tables/Table_5", append tex(frag) dec(3) label  keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

* reduced form 
reghdfe mean_wh_margin share_others_treated postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip if year<2019, absorb(year_month StID) cluster(mktid)
	sum mean_wh_margin if treat_group==0  
	scalar outcome_var_mean = r(mean) 
outreg2 using "$P_Tables/Table_5", append tex(frag) dec(3) label  keep(share_others_treated)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

reghdfe mean_price share_others_treated postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019, absorb(year_month StID) cluster(mktid)
	sum mean_price if treat_group==0  
	scalar outcome_var_mean = r(mean) 
outreg2 using "$P_Tables/Table_5", append tex(frag) dec(3) label  keep(share_others_treated)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

******************  
*** TABLE 6 *****
****************** 

quietly{
	areg treat share_others_treated postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip i.year_month if age>12, absorb(StID)
capture drop treat_pred 
predict treat_pred
label variable treat_pred "Adopter $\times$ post-Adoption"
}

* IV regression 
ivreghdfe mean_wh_margin treat_pred postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip (treat_m* =IV_m*) if year<2019 & age>12, absorb(year_month StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_6", replace tex(frag) dec(3) label  keep(treat_pred treat_m*)  


ivreghdfe mean_price treat_pred postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip (treat_m* =IV_m*) if year<2019 & age>12, absorb(year_month StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_6", append tex(frag) dec(3) label  keep(treat_pred treat_m*)  

** reduced form regression 
label variable share_others_treated "Share Brand Adopters $\times$ post-Adoption"
reghdfe mean_wh_margin share_others_treated IV_m* postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019 & age>12, absorb(year_month StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_6", append tex(frag) dec(3) label  keep(share_others_treated IV_m*)  

reghdfe mean_price share_others_treated IV_m* postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  if year<2019 & age>12, absorb(year_month StID) cluster(mktid) 
	outreg2 using "$P_Tables/Table_6", append tex(frag) dec(3) label  keep(share_others_treated IV_m*)  


****************** 
*** TABLE 7 ***
****************** 

* monopoly markets 
capture drop outcome_var
gener outcome_var = mean_wh_margin 
ivreghdfe mean_wh_margin  mkt_n_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_7",  tex(frag) dec(3) label nor2 replace keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

 capture drop outcome_var
gener outcome_var = mean_price 
 ivreghdfe mean_price  mkt_n_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
 sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_7",  tex(frag) dec(3) label nor2 append  keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)
 
 * non-monopoly markets 
capture drop outcome_var
gener outcome_var = mean_wh_margin 
ivreghdfe mean_wh_margin  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_7",  tex(frag) dec(3) label nor2 append keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

 capture drop outcome_var
gener outcome_var = mean_price 
 ivreghdfe mean_price  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
 sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_7",  tex(frag) dec(3) label nor2 append  keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

****************** 
*** TABLE 8 ***
****************** 


merge 1:1 StID year_month using "$P_Data_Processed/mean_hourly_prices.dta"
keep if _merge==3
drop _merge

label variable mean_price_9 "Mean Price at 9am"
label variable mean_price_12 "Mean Price at Noon"
label variable mean_price_17 "Mean Price at 5pm"
label variable mean_price_19 "Mean Price at 7pm"


local i = 1

foreach var of varlist mean_price_9 mean_price_12 mean_price_17 mean_price_19 {
	
	capture drop outcome_var
	gener outcome_var = `var' 
	ivreghdfe `var'  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
	
	sum outcome_var if e(sample)==1 & treat_group==0
	scalar outcome_var_mean = r(mean)
	 
	if `i'==1 {	
		outreg2 using "$P_Tables/Table_8a",  tex(frag) dec(3) label addstat(Non-Adopter Mean Outcome,outcome_var_mean) replace nor2 keep(treat)
	}
	else {
		outreg2 using "$P_Tables/Table_8a",  tex(frag) dec(3) label addstat(Non-Adopter Mean Outcome,outcome_var_mean) append nor2 keep(treat)

		
	}

	local i = `i'+1
}

local i = 1

foreach var of varlist mean_price_9 mean_price_12 mean_price_17 mean_price_19 {
	
	capture drop outcome_var
	gener outcome_var = `var' 
	ivreghdfe `var'   n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
	
	sum outcome_var if e(sample)==1 & treat_group==0
	scalar outcome_var_mean = r(mean)
	 
	if `i'==1 {	
		outreg2 using "$P_Tables/Table_8b",  tex(frag) dec(3) label addstat(Non-Adopter Mean Outcome,outcome_var_mean) replace nor2
	}
	else {
		outreg2 using "$P_Tables/Table_8b",  tex(frag) dec(3) label addstat(Non-Adopter Mean Outcome,outcome_var_mean) append nor2

		
	}

	local i = `i'+1
}

****************** 
*** TABLE 9 ***
****************** 

clear 

use "$P_Data_Processed/monthly_duo_trio_mkt_data_e5_merged.dta"

* 2sls 
capture drop outcome_var
gener outcome_var = market_mean_wh_margin
	ivreghdfe market_mean_wh_margin l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1 T2 = IV1 IV2) if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
	 sum outcome_var if T1==0 & T2==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 replace  keep(T1 T2) addstat(Zero Adopter Mean Outcome,outcome_var_mean)


	capture drop outcome_var
gener outcome_var = market_mean_price	
	ivreghdfe market_mean_price l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1 T2 = IV1 IV2) if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
sum outcome_var if T1==0 & T2==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 append  keep(T1 T2) addstat(Zero Adopter Mean Outcome,outcome_var_mean)
 
 
* 1st stage 
reghdfe T1 IV1 IV2 l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 append  keep(IV1 IV2)
 
reghdfe T2 IV1 IV2 l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 append  keep(IV1 IV2)

* reduced form 
	capture drop outcome_var
gener outcome_var = market_mean_wh_margin
	reghdfe market_mean_wh_margin IV1 IV2 l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3  if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
	 sum outcome_var if T1==0 & T2==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 append  keep(IV1 IV2) addstat(Zero Adopter Mean Outcome,outcome_var_mean)


	capture drop outcome_var
gener outcome_var = market_mean_price	
	reghdfe mean_price IV1 IV2 l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 if  use_mkt==1 , absorb(mktid year_month)  cluster(mktid) 
sum outcome_var if T1==0 & T2==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_9",  tex(frag) dec(3) label nor2 append  keep(IV1 IV2) addstat(Zero Adopter Mean Outcome,outcome_var_mean)

 

****************** 
*** TABLE 10 ***
****************** 

	ivreghdfe market_mean_wh_margin l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1_6month* T2_6month* = IV1_6month* IV2_6month*) if  use_mkt==1 & sample==1, absorb(mktid year_month)  cluster(Brand) 
	 outreg2 using "$P_Tables/Table_10",  tex(frag) dec(3) label nor2 replace  keep(T1_6month* T2_6month* )
	 
ivreghdfe market_mean_price l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1_6month* T2_6month* = IV1_6month* IV2_6month*) if  use_mkt==1 & sample==1  , absorb(mktid year_month)  cluster(Brand) 
outreg2 using "$P_Tables/Table_10",  tex(frag) dec(3) label nor2 append  keep(T1_6month* T2_6month* )

	ivreghdfe mean_DD_5min_share l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1_6month* T2_6month* = IV1_6month* IV2_6month*) if  use_mkt==1 & sample==1 , absorb(mktid year_month)  cluster(Brand) 
		outreg2 using "$P_Tables/Table_10",  tex(frag) dec(3) label nor2 append  keep(T1_6month* T2_6month* )

		ivreghdfe mean_UU_5min_share l_gdp pop_density med_age employed_share l_pop   mean_temp sd_temp mean_precip sd_precip n_brand1 n_brand2 n_brand3 (T1_6month* T2_6month* = IV1_6month* IV2_6month*) if  use_mkt==1 & sample==1, absorb(mktid year_month)  cluster(Brand) 
			outreg2 using "$P_Tables/Table_10",  tex(frag) dec(3) label nor2 append  keep(T1_6month* T2_6month* )
 
 
